package com.qyhl.shop.shop.shop;

import com.google.gson.Gson;
import com.qyhl.shop.common.ShopUrl;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.base.BaseStringBean;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.shop.ShopDetailBean;

public class ShopDetailModel implements ShopDetailContract.ShopDetailModel {
    private ShopDetailPresenter mPresenter;

    public ShopDetailModel(ShopDetailPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public void getDetail(int shopId, String phoneNum) {
        EasyHttp.post(ShopUrl.SHOP_SHOP_DETAIL)
                .params("shopId", shopId + "")
                .params("phoneNum", phoneNum)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .execute(new SimpleCallBack<ShopDetailBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.setError("获取商铺详情失败！");
                    }

                    @Override
                    public void onSuccess(ShopDetailBean bean) {
                        mPresenter.setDetail(bean);
                    }
                });
    }

    @Override
    public void getCoupon(String phoneNum, int couponId, String sign) {
        EasyHttp.post(ShopUrl.SHOP_COUPON_GET)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("phoneNum", phoneNum)
                .params("couponId", couponId + "")
                .params("sign", sign)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.getCouponResult(e.getMsg());
                    }

                    @Override
                    public void onSuccess(String str) {
                        BaseStringBean response = new Gson().fromJson(str, BaseStringBean.class);
                        mPresenter.getCouponResult(response.getMsg());
                    }
                });
    }

    @Override
    public void followShop(String phoneNum, int shopId, String sign) {
        EasyHttp.post(ShopUrl.SHOP_SHOP_FOLLOW)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("phoneNum", phoneNum)
                .params("shopId", shopId + "")
                .params("sign", sign)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.followResult(e.getMsg(),true);
                    }

                    @Override
                    public void onSuccess(String str) {
                        BaseStringBean response = new Gson().fromJson(str, BaseStringBean.class);
                        mPresenter.followResult(response.getMsg(),true);
                    }
                });
    }

    @Override
    public void unFollowShop(String phoneNum, int shopId, String sign) {
        EasyHttp.post(ShopUrl.SHOP_SHOP_UNFOLLOW)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("phoneNum", phoneNum)
                .params("shopId", shopId + "")
                .params("sign", sign)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.followResult(e.getMsg(),false);
                    }

                    @Override
                    public void onSuccess(String str) {
                        BaseStringBean response = new Gson().fromJson(str, BaseStringBean.class);
                        mPresenter.followResult(response.getMsg(),false);
                    }
                });
    }
}
